package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcgyl.export.entity.Goods;
import com.jcgyl.export.entity.GoodsQueryModel;
import com.jcgyl.export.entity.Tbuploadinfo;
import com.jcgyl.export.mapper.TbuploadinfoMapper;
import com.jcgyl.export.service.GoodsService;
import com.jcgyl.export.service.RedisService;
import com.jcgyl.export.util.POIExcelHelper;
import com.jcgyl.export.util.entity.Result;

import jxl.Cell;

@RestController
@RequestMapping("/goods")
public class GoodsInfoController {
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private RedisService redisService;
	@Autowired
	private TbuploadinfoMapper tbuploadinfoMapper;

	// 获取所有的商品信息
	@PostMapping("/findAllGoods")
	public Map<String, Object> doFindAllGoods(@RequestBody GoodsQueryModel Filter) {
		String goodsname = Filter.getFilter().getGoodsname();
		String goodscode = Filter.getFilter().getGoodscode();
		String hscode = Filter.getFilter().getHscode();
		int pageIndex = Filter.getPageIndex();
		int pageSize = Filter.getPageSize();
		System.out.println("pageIndex:" + pageIndex + "pageSize:" + pageSize);
		Map<String, Object> map = goodsService.findAllGoods(goodsname, goodscode, hscode, pageSize, pageIndex);
		return map;
	}

	// 获取所有的角色信息
	@PostMapping("/process/{uploadId}")
	public Result doFindGoods(@PathVariable("uploadId") int uploadId) {
		try {
			if (tbuploadinfoMapper.selectByPrimaryKey(uploadId) == null) {
				return Result.build(1, "未找到文件路径");
			}
			;
			Tbuploadinfo upload = tbuploadinfoMapper.selectByPrimaryKey(uploadId);
			String file_path = upload.getFilePath();
			List<Cell[]> cell_lists = POIExcelHelper.jxlGetExcelRows(file_path, true);
			if (cell_lists.size() == 0) {
				return Result.build(1, "此Excel中没有数据");
			}
			System.out.println("数组的长度" + cell_lists.size());
			for (int i = 0; i < cell_lists.size(); i++) {
				Cell[] cell_arr = cell_lists.get(i);
				System.out.println(cell_arr.length);
				if (cell_arr[0].getContents().isEmpty()) {
					System.out.println("第" + (i + 2) + "行商品名称没有填写");
					return Result.build(1, "第" + (i + 2) + "行商品名称没有填写");
				}
				if (cell_arr[1].getContents().isEmpty()) {
					System.out.println("第" + (i + 2) + "行商品编码没有填写");
					return Result.build(1, "第" + (i + 2) + "行商品编码没有填写");
				}
				if (cell_arr[2].getContents().isEmpty()) {
					System.out.println("第" + (i + 2) + "行计量单位没有填写");
					return Result.build(1, "第" + (i + 2) + "行计量单位没有填写");
				}
				if (cell_arr[3].getContents().isEmpty()) {
					System.out.println("第" + (i + 2) + "行第一法定计量单位没有填写");
					return Result.build(1, "第" + (i + 2) + "行第一法定计量单位没有填写");
				}
				Goods goods = goodsService.findGoodsByGoodsName(cell_arr[0].getContents());
				if (goods != null) {
					goods.setGoodsname(cell_arr[0].getContents());
					goods.setGoodscode(cell_arr[1].getContents());
					goods.setUnit(cell_arr[2].getContents());
					goods.setUnit1(cell_arr[3].getContents());
					if (cell_arr.length > 4) {
						goods.setUnit2(cell_arr[4].getContents());
					}
					goodsService.updateGoods(goods);
				} else {
					Goods goods_add = new Goods();
					goods_add.setGoodsname(cell_arr[0].getContents());
					goods_add.setGoodscode(cell_arr[1].getContents());
					goods_add.setUnit(cell_arr[2].getContents());
					goods_add.setUnit1(cell_arr[3].getContents());
					if (cell_arr.length > 4) {
						goods_add.setUnit2(cell_arr[4].getContents());
					}
					goodsService.addGoods(goods_add);
				}
			}
			return Result.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getMessage());
		}
	}

	@PostMapping("/updateGoods")
	public Result doUpdateGoods(Goods goods) {
		String goodsname = goods.getGoodsname();
		redisService.set(goodsname, goods);
		return goodsService.updateGoods(goods);
	}

	@PostMapping("/deleteGoods")
	public Result doDeleteGoods(@RequestBody Goods goods) {
		return goodsService.deleteGoods(goods);
	}

}
